package com.ruoyi.kjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.kjy.domain.BannerItem;
import com.ruoyi.kjy.domain.CreateSpace;
import com.ruoyi.kjy.domain.HighTechEnterprisesInfo;
import com.ruoyi.kjy.service.CreateSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Objects;

/**
 * @Description:
 * @Author: Hi，Wang
 * @Date: 10/10,0010 20:19
 */

@RestController
@RequestMapping("/createSpace")
public class CreateSpaceController extends BaseController {


    @Autowired
    private CreateSpaceService createSpaceService;

    @GetMapping("/list")
    public AjaxResult list(Long current, Long size, CreateSpace createSpace) {
        QueryWrapper<CreateSpace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(CreateSpace::getUpdateTime);
        queryWrapper.setEntity(createSpace);
        if (Objects.isNull(current)) {
            current = 1l;
        }
        if (Objects.isNull(size)) {
            size = 10l;
        }
        if (size >= 20l) {
            size = 20l;
        }
        return AjaxResult.success(createSpaceService.page(new Page<>(current, size), queryWrapper));
    }




    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(createSpaceService.getById(id));
    }


    @GetMapping(value = "my")
    public AjaxResult getInfo() {
        return AjaxResult.success(createSpaceService.list(new QueryWrapper<CreateSpace>().lambda().eq(CreateSpace::getUserId, SecurityUtils.getUserId()).orderByDesc(CreateSpace::getUpdateTime)));
    }

    @Log(title = "众创空间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CreateSpace createSpace) {
        createSpace.setStatus(0);
        createSpace.setUserId(SecurityUtils.getUserId());
        boolean save = createSpaceService.save(createSpace);
        return toAjax(save);
    }

    @Log(title = "众创空间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CreateSpace createSpace) {
        return toAjax(createSpaceService.updateById(createSpace));
    }

    @Log(title = "众创空间", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(createSpaceService.removeByIds(Arrays.asList(ids)));
    }




}
